package com.method;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Environment;
import android.util.Base64;
import android.util.Log;


public class CommonMethodSoap {


	public static boolean isNetAvaliable(Context context) {
		ConnectivityManager connectivity = (ConnectivityManager) context
				.getSystemService(context.CONNECTIVITY_SERVICE);
		if (connectivity == null) {
			Log.i("NetWorkState", "Unavailable");
			return false;
		} else {
			NetworkInfo[] info = connectivity.getAllNetworkInfo();
			if (info != null) {
				for (int i = 0; i < info.length; i++) {
					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						Log.i("NetWorkState", "avaliable");
						return true;
					}
				}
			}
		}

		return false;
	}

	public static File getFile() {
		File image = new File(
				Environment
						.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
				"Diary_img");
		if (!image.exists()) {
			image.mkdirs();
		}
		return image;
	}

	// ��ӵ�ͼ��
	public static void addIntoPic(Context context, String path) {
		Intent mediaScanItent = new Intent(
				Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
		File f = new File(path);
		Uri uri = Uri.fromFile(f);
		mediaScanItent.setData(uri);
		context.sendBroadcast(mediaScanItent);
	}

	// ���·�����ͼƬ��ѹ������bitmap������ʾ
	public static Bitmap getSmallBitmap(String filepath) {
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(filepath, options);
		options.inSampleSize = CommonMethodSoap.calculateInSampleSize(options,
				200, 280);
		options.inJustDecodeBounds = false;
		return BitmapFactory.decodeFile(filepath, options);
	}

	public static Bitmap yasuo(Bitmap bitmap) {
		int height = bitmap.getHeight();
		int width = bitmap.getWidth();
		int wheight = 100;
		int wwidth = 83;
		float scaleWidth = ((float) wwidth) / width;
		float scaleheight = ((float) wheight) / height;
		Matrix mat = new Matrix();
		mat.postScale(scaleWidth, scaleheight);
		return Bitmap.createBitmap(bitmap, 0, 0, width, height, mat, true);
	}

	public static Bitmap compross(Bitmap bitmap) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bitmap.compress(CompressFormat.JPEG, 100, baos);// ����ѹ������100��ʾ��ѹ������ѹ�������ݴ�ŵ�byarraystream��
		int option = 10;
		while (baos.toByteArray().length / 1024 > 100) {// ѭ���Ƿ����100kb�����ڵĻ�����ѹ��
			baos.reset();// ����baos�����baos
			option -= 10;
			bitmap.compress(Bitmap.CompressFormat.JPEG, option, baos);
		}
		ByteArrayInputStream bins = new ByteArrayInputStream(baos.toByteArray());// ��ѹ����ͼƬ��ŵ�bytearrayinputstream��
		Bitmap b = BitmapFactory.decodeStream(bins, null, null);// ��bins������ͼƬ
		return b;

	}

	// ��ȡͼƬ��С
	public static int getsize(Bitmap ma) {
		ByteArrayOutputStream ba = new ByteArrayOutputStream();
		ma.compress(CompressFormat.JPEG, 100, ba);
		return (int) (ba.toByteArray().length / 1024);
	}

	// ��ȡ·���µ�ͼƬ
	public static Bitmap getPic(String path) {
		return BitmapFactory.decodeFile(path);
	}

	// ����ͼƬ������ֵ
	public static int calculateInSampleSize(BitmapFactory.Options options,
			int reqWidth, int reqHeight) {
		final int height = options.outHeight;
		final int width = options.outWidth;
		int inSampleSize = 1;
		if (height > reqHeight || width > reqHeight) {
			final int heightRatio = Math.round((float) height
					/ (float) reqHeight);
			final int widthRatio = Math.round((float) width / (float) reqWidth);
			inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
		}
		return inSampleSize;
	}

	// ���·��ɾ��ͼƬ
	public static void deleteBitmap(String path) {
		File f = new File(path);
		if (f.exists()) {
			f.delete();
		}
	}

	// ��bitmapת��Ϊstring
	public static String bitmapToString(Bitmap bitmap) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bitmap.compress(Bitmap.CompressFormat.JPEG, 60, baos);
		byte[] b = baos.toByteArray();
		return Base64.encodeToString(b, Base64.DEFAULT);
	}

	// �ж�gprs�Ƿ��
	public static boolean isOpenGPS(Context context) {
		LocationManager locationmanager = (LocationManager) context
				.getSystemService(Context.LOCATION_SERVICE);
		// ͨ��GPS���Ƕ�λ����λ������Ծ�ȷ���֣�ͨ��24�����Ƕ�λ��������Ϳտ��ĵط���λ׼ȷ���ٶȿ죩
		boolean gps = locationmanager
				.isProviderEnabled(LocationManager.GPS_PROVIDER);
		// ͨ��WLAN���ƶ�����(3G/2G)ȷ����λ�ã�Ҳ����AGPS������GPS��λ����Ҫ���������ڻ��ڸ������Ⱥ��ï�ܵ����ֵȣ��ܼ��ĵط���λ��
		// boolean network = locationmanager
		// .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
		if (gps) {
			return true;
		}
		return false;

	}

}
